package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientPlant;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.mapper.ClientPlantMapper;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper mapper;
    @Autowired
    private ClientPlantMapper clientPlantMapper;
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }


    public void insertBrand(Client record) {
        if (record.getId() == null) {
            //id不存在 也就是state==2潜在客户 可开发
            //还要设置当前用户的id  和  录入人的id 设置为4L
    Employee currentUser = (Employee)SecurityUtils.getSubject().getPrincipal();

            record.setInputuser(currentUser);
            record.setPrincipal(currentUser);
            record.setState(Client.STATE_LATENT);//2
            record.setDevelopsgin(Client.DEVELOPSIGN_SUCCESS);//设置为可以开发;
            insert(record);
        }
    }


    public int insert(Client record) {

        return mapper.insert(record);
    }

    public void insertOfficalClient(Client record) {

    }

    public void insertClientSource(Client record) {

    }

    public Client selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Client> selectAll() {
        return mapper.selectAll();
    }

    public int updateByPrimaryKey(Client record) {
        if (record.getState() != Client.STATE_OFFI && record.getDevelopsgin() != null) {
            //潜在客户 需要标识
        } else if (record.getState() == Client.STATE_OFFI) {
            //正式员工
            record.setDevelopsgin(null);

        } else if (record.getState() == Client.STATE_SOURCE) {
            //资源池中的客户
            record.setDevelopsgin(null);

        }

        return mapper.updateByPrimaryKey(record);
    }

    public int queryForCount(ClientQueryObject qo) {
        return mapper.queryForCount(qo);
    }

    public List<Client> queryForList(ClientQueryObject qo) {
        return mapper.queryForList(qo);
    }

    public PageResult query(ClientQueryObject qo) {
        int count = mapper.queryForCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.emptyList());
        }
        List<Client> list = mapper.queryForList(qo);
        return new PageResult(count, list);
    }

    public void absorb(Long id) {
        //标识改为可开发
        Employee currentUser = (Employee)SecurityUtils.getSubject().getPrincipal();
        Client client = selectByPrimaryKey(id);
        client.setDevelopsgin(Client.DEVELOPSIGN_SUCCESS);
        //状态改为 潜在客户2
        client.setState(Client.STATE_LATENT);
        //0责任人和操作人改为当前用户
        client.setInputuser(currentUser);
        client.setPrincipal(currentUser);
        mapper.absorb(client);
    }
    /**
     * 查询正式客户信息
     * 返回PageResult,可以分页
     * */
    public PageResult queryFormalClient(ClientQueryObject qo) {
        int count = mapper.queryForFormalClientCount(qo);
        if(count==0){
            return new PageResult(0, Collections.emptyList());
        }
        List<Client> list = mapper.queryForFormalClientList(qo);
        return new PageResult(count,list);
    }

    public Client queryClientIdsStoraId(Long stoId) {
        return mapper.queryClientIdsStoraId(stoId);
    }

    public List<Client> allLatentClient(int state) {
        return mapper.queryForXls(state);
    }

    public List<Client> allLatentClient(Integer state) {
        return mapper.queryForXls(state);
    }


    //将客户移交到资源池
    public void movesource(Long id) {
        Client client = selectByPrimaryKey(id);
        //将状态改成
        client.setDevelopsgin(null);
        //设置操作时间
        client.setInputdate(new Date());
        //状态改为 资源池客户0
        client.setState(Client.STATE_SOURCE);
        mapper.movesource(client);
    }

    public void admovesource(Long id) {
        Client client = selectByPrimaryKey(id);
        Integer developsgin = client.getDevelopsgin();
        //将状态改成
        if(developsgin!=null) {
            if (developsgin== Client.DEVELOPSIGN_SUCCESS) {
                client.setDevelopsgin(Client.DEVELOPSIGN_FAIL);
            } else {
                client.setDevelopsgin(Client.DEVELOPSIGN_SUCCESS);
            }
            mapper.admovesource(client);
        }
    }
//将客户从潜在客户中移到开发表中
    public void tryexploit(Long id) {
        try {
            Client client = selectByPrimaryKey(id);
            ClientPlant clientPlant =new ClientPlant();
            //尝试开发 需要设置state3  然后根据 3 查询
            client.setState(Client.STATE_PLAN);
            //客户id
            clientPlant.setClient(client);
            //客户责任人id
            clientPlant.setDuty(client.getPrincipal());
            System.out.println(clientPlant.getDuty());
            //获取当前用户 的  id
            Employee currentUser = (Employee)SecurityUtils.getSubject().getPrincipal();
            clientPlant.setOperator(currentUser);
            //获取当前时间
            Date creattime = new Date();
            clientPlant.setCreattime(creattime);
            //保存到开发表中
            clientPlantMapper.insert(clientPlant);
            mapper.movesource(client);//将状态改成3
            System.out.println(clientPlant);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }



